package com.zhao.service.serviceImpl;

import com.zhao.dao.StudentDao;
import com.zhao.dao.UniversityDao;
import com.zhao.pojo.Student;
import com.zhao.pojo.University;
import com.zhao.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

@Service
@Transactional
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private UniversityDao universityDao;

    //验证学生身份，验证是否已经注册过，
    @Override
    public String studentRegister(String StudentID, String password, String username, String universityID) {

        System.out.println("service  register");
        Student student=studentDao.findByStudentID(StudentID,universityID);
        if(student!=null){
            System.out.println(student);
            if(username.equals(student.getName())&&student.getIsRegisted().equals("0")){
                student.setPassWord(password);
                student.setUniversityID(universityID);
                //密码加密
                //String newPassWord= DigestUtils.md5DigestAsHex(student.getPassWord().getBytes(StandardCharsets.UTF_8));
                //student.setPassWord(newPassWord);

                //标记已经注册  (该操作推迟到用户首次登录，并完善信息之后)
                //student.setIsRegisted("1");

                //修改用户信息
                System.out.println(student);
                int res=studentDao.updateStudent(student);
                if(res==1){
                    return "success";
                }

            }else if(!username.equals(student.getName())){
                System.out.println("学生姓名错误！");
                return "学生姓名错误!";
            }else if(student.getIsRegisted().equals("1")){
                System.out.println("已经注册过了！");
                return "已经注册过了！";
            }
        }

        System.out.println("学号错误");
        return "学号错误!";


    }
}
